{% extends 'TopxiaWebBundle::bootstrap-modal-layout.html.twig' %}

{% set billable = true %}
{% if (
  (setting('coin.coin_enabled') == 1 and setting('coin.price_type') == 'Coin' and course.price == 0)
  or (( not setting('coin.price_type') or setting('coin.price_type') == "RMB" ) and course.price == 0 )
  ) %}
  {% set billable = false %}
{% endif %}
{% block title %}{% if billable %}{{'购买课程'|trans}}{% else %}{{'加入学习'|trans}}{% endif %}{% endblock %}
{% block body %}

  {% if billable and course.type=="live" %}
    <div class="alert alert-info">{{'注意：请在30分钟内完成支付，否则订单将会过期失效'|trans}}</div>
  {% endif %}
  <form id="course-buy-form" class="form-horizontal" method="post"
  action="{{ path('course_buy_modify_user_info') }}"
  >
    <div class="form-group">
      <div class="col-sm-3 control-label">{{'课程名称'|trans}}</div>
      <div class="col-sm-9 controls">
      	<span class="control-text text-muted">《{{ course.title }}》</span>
        <input type="hidden" name="lessonId" value="{{ lessonId}}">
      </div>
    </div>

    {% if billable %}
    <div class="form-group">
      <div class="col-sm-3 control-label">{{'价格'|trans}}</div>
      <div class="col-sm-9 controls money-text">
      	<span class="control-text">
          {% if vipStatus == 'ok' %}
            {{'会员免费'|trans}}
          {% else %}
            {% if setting('coin.coin_enabled') and setting('coin.price_type') == 'Coin' %}
            <strong class="money">{{ course.price * setting('coin.cash_rate')}}</strong>
            <span class="text-muted">{{setting('coin.coin_name')}}</span>
            {% else %}
            <strong class="money">{{ course.price }}</strong>
        		<span class="text-muted">{{'元'|trans}}</span>
            {% endif %}
          {% endif %}
      	</span>
      </div>
    </div>
    {% endif %}

    {% if billable %}
      {% if not payments %}
        <div class="alert alert-info">{{ setting('payment.disabled_message')|default('尚未开启支付模块，无法购买课程。'|trans)|raw }}</div>
      {% endif %}
    {% else %}
      {% if avatarAlert %}
        {% set goto =  path('course_show', {id:course.id}) %}
        <div class="alert alert-warning">{{'您还没有头像，设置以后才能加入学习'|trans}}<br />{{'拥有一个独有的头像，老师和同学们能更容易关注到你哦～～'|trans}} <a href="{{ path('settings_avatar',{goto: goto}) }}" class="alert-link">{{'点击设置'|trans}}</a></div>
      {% else %}
        <div class="alert alert-info">{{'此课程为免费课程，无需购买，可直接加入学习。'|trans}}</div>
      {% endif %}
    {% endif %}
    <input type="hidden" name="targetId" value="{{ course.id }}" />
    <input type="hidden" name="payment" value="alipay" />
    <input type="hidden" name="_csrf_token" value="{{ csrf_token('site') }}">

    {% set userinfo_enable = setting('course.buy_fill_userinfo')|default(false) %}
    {% set checkedFields = setting('course.userinfoFields')|default([]) %}
    {% include 'TopxiaWebBundle:User:fill-userinfo-fields-block.html.twig' with {isBuyFillUserinfo:userinfo_enable, userinfoFieldsSetting:checkedFields, userFields:userFields, user:user, showNavTip:1} %}

  </form>
{% endblock %}

{% block footer %}
  {% set priceType = setting("coin.price_type") %}
  {% if billable and ((priceType|default("RMB") == "RMB" and course.price > 0) or (priceType|default("RMB") == "Coin" and course.price > 0)) %}
    {% if payments %}
      {% if member %}
        <span class="text-muted">{{'预览模式无法支付'|trans}}</span>
        <button class="btn btn-primary" disabled="disabled">{{'去支付'|trans}}</button>
      {% else %}
        {% if vipStatus == 'ok'%}
           <button id="join-course-btn" class="btn btn-primary" data-loading-text="{{'正在加入'|trans}}..." type="submit" data-toggle="form-submit" data-target="#course-buy-form">{{'加入学习'|trans}}</button>
        {% else %}
        <button id="course-pay" class="btn btn-primary" type="submit" data-loading-text="{{'正在创建订单'|trans}}..." data-toggle="form-submit" data-target="#course-buy-form">{{'去支付'|trans}}</button>
        {% endif %}
      {% endif %}
    {% elseif not payments and vipStatus == 'ok' %}
      <button id="join-course-btn" class="btn btn-primary"
        {% if course.status != 'published' or (course.type == 'live' and course.studentNum >= course.maxStudentNum) %}
        disabled="disabled"
        {% endif %}
        data-loading-text="{{'正在加入'|trans}}..." data-submiting-text="{{'正在加入'|trans}}..." type="submit" data-toggle="form-submit" data-target="#course-buy-form">{{'加入学习'|trans}}</button>
    {% endif %}
  {% else %}
    {% if member %}
      <span class="text-muted">{{'预览模式无法加入学习'|trans}}</span>
      <button class="btn btn-primary" disabled="disabled">{{'加入学习'|trans}}</button>
    {% else %}
      {% if not avatarAlert %}
        <button id="join-course-btn" class="btn btn-primary"
        {% if course.status != 'published' or (course.type == 'live' and course.studentNum >= course.maxStudentNum) %}
        disabled="disabled"
        {% endif %}
        data-loading-text="{{'正在加入'|trans}}..." data-submiting-text="{{'正在加入'|trans}}..." type="submit" data-toggle="form-submit" data-target="#course-buy-form">{{'加入学习'|trans}}</button>
      {% endif %}
    {% endif %}

  {% endif %}

  <script>
    app.load('course/buy-modal');
  </script>
{% endblock %}